package com.redsun.smallworld.gl.glcontroller;

import com.redsun.smallworld.gl.controls.moudle.Scene;
import com.redsun.smallworld.gl.controls.moudle.SceneList;
import com.redsun.smallworld.gl.controls.parent.SuperControl;
import com.redsun.smallworld.service.RFListener;
import com.redsun.smallworld.tools.JsonUtil;

import javax.bluetooth.*;
import javax.media.opengl.GL2;
import java.io.*;
import java.util.List;


/**
 * Created by zhebinwang on 2017/6/2.
 */
public class DisPlayController {


    private final static DisPlayController disPlayController =new DisPlayController();

    private static int scenecur=0;

    public  static DisPlayController  getInterface(){

        return disPlayController;
    }

    private DisPlayController(){

    }

    public static Scene[] scenes;
    public List<Scene> sceneList;


    /**一次性根据json文件 ，将场景和控件全部装入*/
    public int initScene(){




        SceneList sceneList=null;
        try {
            //通过一个函数读取文件
             sceneList=JsonUtil.extractData(doReadFile().toString(), SceneList.class);
        } catch (Exception e) {
            e.printStackTrace();
        }

        scenes=new Scene[sceneList.getSceneList().size()];
        for (int i=0;i<sceneList.getSceneList().size();i++){
            int x=sceneList.getSceneList().get(i).getSceneId();
            scenes[x]=new Scene();
            scenes[x].setSceneId(x);
            scenes[x].setSceneName(sceneList.getSceneList().get(i).getSceneName());
            scenes[x].setControlInfoList(sceneList.getSceneList().get(i).getControlInfoList());
            scenes[x].fullSuperControls();

        }

        /*Scene tmp=new Scene();
        tmp.setSceneId(1);
        tmp.setSceneName("main");
        List<ControlInfo> controlInfos= Lists.newArrayList();
        ControlInfo controlInfo=new ControlInfo();
        controlInfo.setHeight(50).setWidth(30).setX(0).setY(10)
                .setTexindex(1)
                .setTexheight(1.0f).setTexwidth(1.0f)
                .setTexu(0.0f).setTexv(0.0f);
        controlInfos.add(controlInfo);
        tmp.setControlInfoList(controlInfos);

        System.out.println(JsonUtil.parseObjToJsonString(tmp));
        System.out.println(System.getProperty("user.dir"));*/


        //scenes[0].loadControlInfo();
        return 0;
    }
    //private int fullScene

    private String doReadFile() throws  Exception{
        String pathName = "/Users/zhebinwang/dev/srces/RedSun/smallworld/target/cfg/scene.txt";
        StringBuilder stringBuilder=new StringBuilder();
        File file=new File(pathName);
        if(file.isFile() && file.exists()) { //判断文件是否存在
            InputStreamReader read = new InputStreamReader(
                    new FileInputStream(file), "GBK");//考虑到编码格式
            BufferedReader bufferedReader = new BufferedReader(read);
            String lineTxt = null;
            while ((lineTxt = bufferedReader.readLine()) != null) {
                stringBuilder.append(lineTxt);
            }
            read.close();
        }
        System.out.println(stringBuilder.toString());
        return stringBuilder.toString();
    }

    public int displayCycle(GL2 gl2){

        List<SuperControl>superControls =scenes[scenecur].getSuperControls();
        for (int x=0;x<superControls.size();x++){
            superControls.get(x).doRender(gl2);
        }

        //gl2.glBindTexture(GL.GL_TEXTURE_2D, 1);
       /* gl2.glBindTexture(GL.GL_TEXTURE_2D, TextureController.textureid[2]);


        gl2.glBegin(GL2.GL_QUADS);
        // 左下
        gl2.glTexCoord2f(0.0f, 1.0f); gl2.glVertex2f(0f, 0f);
        // 右下
        gl2.glTexCoord2f(1.0f, 1.0f); gl2.glVertex2f(100f, 0f);
        // 右上
        gl2.glTexCoord2f(1.0f, 0.0f); gl2.glVertex2f(100f, 100f);
        // 左上
        gl2.glTexCoord2f(0.0f, 0.0f); gl2.glVertex2f(0f, 100f);
        gl2.glEnd();



        gl2.glBindTexture(GL.GL_TEXTURE_2D, TextureController.textureid[1]);

        gl2.glBegin(GL2.GL_QUADS);
        // 左下
        gl2.glTexCoord2f(0.0f, 1.0f); gl2.glVertex2f(0f, 0f);
        // 右下
        gl2.glTexCoord2f(1.0f, 1.0f); gl2.glVertex2f(100f, 0f);
        // 右上
        gl2.glTexCoord2f(1.0f, 0.0f); gl2.glVertex2f(100f, 50f);
        // 左上
        gl2.glTexCoord2f(0.0f, 0.0f); gl2.glVertex2f(0f, 50f);
        gl2.glEnd();*/


        return 1;
    }

    public void createBlue(){
        // 获取手机默认上的蓝牙适配器
        LocalDevice localdev =null;
        RFListener rfListener=new RFListener();
        try {
            localdev = LocalDevice.getLocalDevice();
        } catch (BluetoothStateException e) {
            System.out.println("ERROR: cannot access local device");
            System.exit(1);
        }
        DiscoveryAgent agent = localdev.getDiscoveryAgent();
        try {
            // startInquiry() is non-blocking.
            agent.startInquiry(DiscoveryAgent.GIAC, rfListener);
        } catch (BluetoothStateException e) {
            System.out.println("ERROR: device inquiry failed");
            System.exit(2);
        }

        // pause for a while
        while (rfListener.getDevFound() == null) {
            try {
                Thread.sleep(5000);
            } catch (Exception e) {

            }
        }
        RemoteDevice rd = rfListener.getDevFound();
        System.out.println(rd.toString());
    }

}
